<?php

/**
 *
 * @author Guy
 */
abstract class IndexerInterface {

    public function __construct() {
        $this->stopList = file(STOP_LISTE, FILE_IGNORE_NEW_LINES);
    }

    /**
     * Retourne les terme d'une phrase donnée en paramètre
     * @param string $request
     * @return string[]
     */
    public function wordsToTerm($request) {

        //Ex: Quelles sont les ? -> ["Quelles", "sont", "les", ""]
        $mot_clefs  = preg_split("/[\s?!.]/", mb_strtolower($request, 'UTF-8'));

        $terms          = array();
        $terms_included = array();
        foreach($mot_clefs as $mot_clef)
        {
            //Ex: d'Omar => ["d'Omar", "d", "Omar"]
            $mots = array_merge(array($mot_clef), preg_split("/['\/\-\-]/iu", $mot_clef));

            foreach($mots as $mot)
            {
                $mot = trim($mot);
                if($this->isTerm($mot))
                {
                    $term = $this->wordToTerm($mot);
                    //Tri du tableau final
                    if(!in_array($term, $terms_included))
                    {
                        $terms_included[]   = $term;
                        $terms[]            = array("term" => $term, "mot" => $mot_clef);
                    }
                }
            }
        }

        return $terms;
    }

    public function isTerm($mot)
    {
        $mot = trim($mot);
        return strlen($mot) > 0 and !in_array($mot, $this->stopList) and preg_match("/[\w\d]/iu", $mot);
    }
    
    /**
     * Retourne le poid d'un terme dans un document
     * @param Document $document
     * @param Occurence $terme
     * @return int
     */
    public abstract function getPoids(\Document $document, \Occurence $terme);
    
    /**
     * Retourne le terme d'un string donné en paramètre
     * @param string $mot
     * @return string
     */
    public abstract function wordToTerm($mot);

}
